package com.atguigu.product.products.service.impl;

import com.atguigu.product.products.dao.PmsAttrAttrgroupRelationDao;
import com.atguigu.product.products.dao.PmsAttrGroupDao;
import com.atguigu.product.products.dao.PmsCategoryDao;
import com.atguigu.product.products.entity.PmsAttrAttrgroupRelationEntity;
import com.atguigu.product.products.vo.reqVo.AttrSaveReqVo;
import com.atguigu.product.products.vo.respVo.AttrQueryRespVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.product.products.dao.PmsAttrDao;
import com.atguigu.product.products.entity.PmsAttrEntity;
import com.atguigu.product.products.service.PmsAttrService;
import org.springframework.transaction.annotation.Transactional;


@Service("pmsAttrService")
public class PmsAttrServiceImpl extends ServiceImpl<PmsAttrDao, PmsAttrEntity> implements PmsAttrService {
    @Autowired
    private PmsAttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    private PmsAttrGroupDao attrGroupDao;
    @Autowired
    private PmsCategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PmsAttrEntity> page = this.page(
                new Query<PmsAttrEntity>().getPage(params),
                new QueryWrapper<PmsAttrEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存数据到属性表和关联表中
     *
     * @param attrSaveReqVo
     */
    @Transactional
    public void saveMain(AttrSaveReqVo attrSaveReqVo) {
        PmsAttrEntity attrEntity = new PmsAttrEntity();
        //新增数据到属性表中
        if (attrSaveReqVo != null) {
            BeanUtils.copyProperties(attrSaveReqVo, attrEntity);
            //新增到属性表中
            this.baseMapper.insert(attrEntity);
            if(attrEntity.getAttrType()==1){
                //新增数据到关联表中
                PmsAttrAttrgroupRelationEntity en = new PmsAttrAttrgroupRelationEntity();
                en.setAttrGroupId(attrSaveReqVo.getAttrGroupId());
                en.setAttrId(attrEntity.getAttrId());
                attrAttrgroupRelationDao.insert(en);
            }


        }
    }

    /**
     * 查询属性分组查询，可以根据分类id查询，暂时不考虑模糊查询
     *
     * @param categoryId
     * @param params
     * @return
     */
    public PageUtils baseList(Long categoryId, Map<String, Object> params,String attr) {
        Integer flag= ("base".equals(attr)) ? 1 : 0;

        QueryWrapper<PmsAttrEntity> wapper = new QueryWrapper();
        wapper.eq("attr_type",flag);
        if (categoryId!= 0) {
            wapper.eq("catelog_id", categoryId);
        }
        IPage<PmsAttrEntity> page = this.page(
                new Query<PmsAttrEntity>().getPage(params),
                wapper
        );
        List<AttrQueryRespVo> attrQueryRespVoList = attrAll(page.getRecords(),flag);
        PageUtils pageUtils=new PageUtils(page);
        pageUtils.setList(attrQueryRespVoList);
        return pageUtils;
    }

    /**
     * 组装数据返回到属性查询界面
     */
     public  List<AttrQueryRespVo>  attrAll(List<PmsAttrEntity> records,Integer flag){
             List<AttrQueryRespVo> attrQueryRespVos = records.stream().map((m) -> {
                 AttrQueryRespVo attrQueryRespVo = new AttrQueryRespVo();
                 BeanUtils.copyProperties(m, attrQueryRespVo);
                 if(flag==1){
                     //获取分组id
                     PmsAttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationDao.selectOne(new QueryWrapper<PmsAttrAttrgroupRelationEntity>().eq("attr_id", m.getAttrId()));
                     //根据分组id查询分组名称
                     if(attrAttrgroupRelationEntity.getAttrGroupId()!=null){
                         attrQueryRespVo.setGroupName(attrGroupDao.selectById(attrAttrgroupRelationEntity.getAttrGroupId()).getAttrGroupName());
                         attrQueryRespVo.setAttrGroupId(attrAttrgroupRelationEntity.getAttrGroupId());
                     }

                 }


                 //根据分类id查询出分类名称
                 attrQueryRespVo.setCatelogName(categoryDao.selectById(m.getCatelogId()).getName());
                 return attrQueryRespVo;
             }).collect(Collectors.toList());
             return attrQueryRespVos;

     }

}